import React, { Component, createContext } from "react";

const { Provider, Consumer: CartConsumer } = createContext();

// 做Provider的二次封装, 将他封装成一个具有特定功能的provider
class CartProvider extends Component {
  state = {
    count: 4,
  };
  add = () => {
    this.setState((prevState) => ({
      count: prevState.count + 1,
    }));
  };
  minus = () => {
    this.setState((prevState) => ({
      count: prevState.count - 1,
    }));
  };
  render() {
    return (
      <Provider
        value={{ count: this.state.count, add: this.add, minus: this.minus }}
      >
        {this.props.children}
      </Provider>
    );
  }
}

export { CartProvider, CartConsumer };
